package com.domain.controller;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.views.AdminView;

public class AdminController {

	private AdminView adminView;
	private ActionListener actionListener;

	public AdminController(AdminView adminView) {

		this.adminView = adminView;

	}

	public void controll() {

		ActionListener actionListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				Object obj = e.getSource();

				if (obj.equals(adminView.getBtnSubmit())) {

					String userName = adminView.getTxtUserName().getText();
					String password = new String(adminView.getTxtPassword()
							.getPassword());

					if (userName.equals("") || password.equals("")) {

						adminView.getLblMessages().setText(
								"Fields with * are mandatory");
						adminView.getLblMessages().setVisible(true);

						if (userName.equals("")) {
							adminView.getLblUserNameError().setVisible(true);
							adminView.getTxtUserName().setBackground(
									Color.LIGHT_GRAY);
						} else {

							adminView.getLblUserNameError().setVisible(false);
							adminView.getTxtUserName().setBackground(
									Color.white);
						}
						if (password.equals("")) {
							adminView.getLblPasswordError().setVisible(true);
							adminView.getTxtPassword().setBackground(
									Color.LIGHT_GRAY);
						} else {
							adminView.getLblPasswordError().setVisible(false);
							adminView.getTxtPassword().setBackground(
									Color.white);
						}

					} else {

						if (userName.equalsIgnoreCase("admin")
								&& password.equals("admin")) {

							adminView.getLblMessages().setText(
									"Welcome Admin! ");
							adminView.getLblMessages().setVisible(true);

						} else {

							adminView.getLblUserNameError().setVisible(false);
							adminView.getLblPasswordError().setVisible(false);

							adminView.getTxtUserName().setBackground(
									Color.white);
							adminView.getTxtPassword().setBackground(
									Color.white);
							adminView.getLblMessages().setText(
									"Sorry! invalid details");
							adminView.getLblMessages().setVisible(true);

						}
					}

				}
				if (obj.equals(adminView.getBtnReset())) {

					adminView.getLblMessages().setVisible(false);
					adminView.getTxtUserName().setText("");
					adminView.getTxtPassword().setText("");
					adminView.getLblUserNameError().setVisible(false);
					adminView.getLblPasswordError().setVisible(false);

					adminView.getTxtUserName().setBackground(Color.white);
					adminView.getTxtPassword().setBackground(Color.white);
					adminView.getTxtUserName().requestFocus();

				}

			}
		};

		adminView.getBtnSubmit().addActionListener(actionListener);
		adminView.getBtnReset().addActionListener(actionListener);

	}
}
